package com.doghome.news.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.doghome.news.dao.TopicMapper;
import com.doghome.news.entity.Topic;
import com.doghome.news.service.NewsService;
import com.doghome.news.service.TopicService;

@Service
@Transactional
public class TopicServiceImpl implements TopicService {
	
	@Autowired
	private TopicMapper topicMapper;
	@Autowired
	private NewsService newsService;

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public List<Topic> allTopic(Integer tid) {
		return topicMapper.selectAll(null);
	}

	@Override
	@Transactional(propagation = Propagation.SUPPORTS)
	public boolean repeateName(String tname) {
		if(topicMapper.selectByName(tname)>0) {
			return true;
		}
		return false;
	}

	@Override
	public boolean addTopic(Topic topic) {
		return topicMapper.insertSelective(topic) == 1 ? true :false;
	}

	@Override
	public boolean modifyTopic(Topic topic) {
		return topicMapper.updateByPrimaryKeySelective(topic) == 1 ? true : false;
	}

	@Override
	public int delTopic(Integer tid) {
		if(newsService.countNewsById(tid)>0) {
			return 3;
		}
		return topicMapper.deleteByPrimaryKey(tid) == 1 ? 1 : 2;
	}

}
